/**
 * 排除Object对象中的空值，只保留非空KV
 * @author ZhouWeiBo
 */
export function reviewObject(object = {}) {
    let result = null;
    if (!object) {
        return result;
    }
    if (object instanceof Array) {
        result = [];
        result = object
            .filter(item => {
                return (item instanceof String && isJSONStr(item)) || (item instanceof Object && isJSONObj(item));
            })
            .map(obj => {
                return reviewObject(obj);
            })
    } else if (object instanceof Object && isJSONObj(object)) {
        result = {};
        Object.keys(object)
            .filter(key => {
                return object[key];
            })
            .forEach(key => {
                result[key] = object[key];
            });
    } else if (object instanceof String && isJSONStr(object)) {
        let Obj = JSON.parse(object);
        result = reviewObject(Obj);
    }
    return result;
}

/**
 * 判断是否是JSONString
 * @author ZhouWeiBo
 * @param string
 * @returns boolean
 */
export function isJSONStr(string) {
    return isJSONObj(JSON.parse(string));
}

/**
 * 判断是否是JSONObject
 * @author ZhouWeiBo
 * @param obj
 * @returns boolean
 */
function isJSONObj(obj) {
    return typeof (obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() === "[object object]" && !obj.length;
}


